﻿using miniQQ_Template;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace miniQQ_Template
{
    internal static class FileHelper
    {
        public static string Folder = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), $"Plugin\\{QQMain.AddOnName}");
        public static bool TryWrite(string fileName,string content)
        {
            try
            {
                if (!File.Exists(Path.Combine(Folder, fileName)))
                {
                    File.Create(Path.Combine(Folder, fileName));
                }
                File.WriteAllText(Path.Combine(Folder, fileName), content);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool TryRead(string fileName, out string content)
        {
            content = string.Empty;
            try
            {
                content = File.ReadAllText(Path.Combine(Folder,fileName));
            }
            catch (Exception ex)
            {
                Api.ApiOutPutLog(ex.Message);
                return false;
            }
            return true;
        }
        public static bool TryReadButNotEmpty(string fileName, out string content)
        {
            content = string.Empty;
            try
            {
                content = File.ReadAllText(Path.Combine(Folder, fileName));
            }
            catch (Exception ex)
            {
                return false;
            }
            if (content == string.Empty)
            {
                return false;
            }
            return true;
        }
        static FileHelper()
        {
            if (!Directory.Exists(Folder))
            {
                Directory.CreateDirectory(Folder);
            }
        }
    }
}
